/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */ 
package controller;



import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

 


/**
 *
 * @author phill_000
 */

public class WriteXMLFileStudent {
 
	public static void main(String studentName, String title, String gender, String officialAdress, 
                    String currentAdress, String email, String phoneNumber, 
                    String matriculationNumber, String studies, int semester, String degree) throws SAXException, IOException, ParserConfigurationException, TransformerConfigurationException, TransformerException {
 
	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("./web/studentXML.xml");
        Element root = document.getDocumentElement();

        Element rootElement = document.getDocumentElement();

        //Collection<Student> std = new ArrayList<Student>();
        //std.add(new Student());

        //for (Student i : std) {
            // server elements
            Element student = document.createElement("Student");
            rootElement.appendChild(student);

            Element name = document.createElement("Name");
            name.appendChild(document.createTextNode(studentName));
            student.appendChild(name);

            Element sStudies = document.createElement("Studies");
            sStudies.appendChild(document.createTextNode(studies));
            student.appendChild(sStudies);

            Integer i = new Integer(semester);
            String s = i.toString();
            Element sSemester = document.createElement("Semester");
            sSemester.appendChild(document.createTextNode(s));
            student.appendChild(sSemester);


            Element sDegree = document.createElement("Degree");
            sDegree.appendChild(document.createTextNode(degree));
            student.appendChild(sDegree);

            Element adress = document.createElement("Adress");
            adress.appendChild(document.createTextNode(officialAdress));
            student.appendChild(adress);

            Element mail = document.createElement("Email");
            mail.appendChild(document.createTextNode(email));
            student.appendChild(mail);
            
            Element ttitle = document.createElement("Title");
            ttitle.appendChild(document.createTextNode(title));
            student.appendChild(ttitle);

            Element ggender = document.createElement("Gender");
            ggender.appendChild(document.createTextNode(gender));
            student.appendChild(ggender);
     
            Element oAdress = document.createElement("OfficialAdress");
            oAdress.appendChild(document.createTextNode(officialAdress));
            student.appendChild(oAdress);
            
            Element cAdress = document.createElement("OfficialAdress");
            cAdress.appendChild(document.createTextNode(currentAdress));
            student.appendChild(cAdress);
        
            Element number = document.createElement("PhoneNumber");
            number.appendChild(document.createTextNode(phoneNumber));
            student.appendChild(number);
        
            Element matNumber = document.createElement("MatriculationNumber");
            matNumber.appendChild(document.createTextNode(matriculationNumber));
            student.appendChild(matNumber);
       

            root.appendChild(student);
        //}
        DOMSource source = new DOMSource(document);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult("./web/studentXML.xml");
        transformer.transform(source, result);
        System.out.println("File2 saved!");
    }
}